<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>股票搜索测试</title>
</head>
<body>
    <h1>股票搜索测试</h1>
    <input type="text" id="stockInput" placeholder="输入股票代码或名称" value="000977">
    <button onclick="testSearch()">搜索</button>
    <div id="result"></div>

    <script>
        async function testSearch() {
            const input = document.getElementById('stockInput').value.trim();
            const resultDiv = document.getElementById('result');

            if (!input) {
                resultDiv.innerHTML = '请输入股票代码或名称';
                return;
            }

            try {
                // 处理股票代码格式
                let code = input.toUpperCase();
                if (/^\d{6}$/.test(code)) {
                    if (code.startsWith('6')) {
                        code = code + '.SH';
                    } else if (code.startsWith('0') || code.startsWith('3')) {
                        code = code + '.SZ';
                    }
                }

                console.log('搜索代码:', code);

                const response = await fetch(`/api/stock/${code}`);
                const result = await response.json();

                console.log('API返回:', result);

                if (result.success && result.data) {
                    const data = result.data;
                    const originalCode = data.code.replace(/\.(SH|SZ)$/, '');

                    resultDiv.innerHTML = `
                        <h3>搜索结果:</h3>
                        <p><strong>股票代码:</strong> ${originalCode}</p>
                        <p><strong>股票名称:</strong> ${data.name}</p>
                        <p><strong>当前价格:</strong> ¥${data.price}</p>
                        <p><strong>涨跌额:</strong> ${data.change}</p>
                        <p><strong>涨跌幅:</strong> ${data.changePercent}%</p>
                    `;
                } else {
                    resultDiv.innerHTML = '未找到该股票';
                }
            } catch (error) {
                console.error('搜索失败:', error);
                resultDiv.innerHTML = '搜索失败: ' + error.message;
            }
        }

        // 页面加载时自动搜索
        window.onload = function() {
            testSearch();
        };
    </script>
</body>
</html>